const path = require('path')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const isProduction = process.env.NODE_ENV === 'production'
const isExperiment = !!process.env.IS_EXPERIMENT
const resolve = target => path.resolve(__dirname, target)
const webpackBundleAnalyzer = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const miniCssExtractPlugin = require('mini-css-extract-plugin')
const webpackConfig = {
  mode: isProduction ? 'production' : 'development',
  entry: resolve('./experiment.js'),
  output: {
    path: resolve('../web-page'),
    filename: '[name].[hash].js',
    publicPath: '/',
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: 'vue-loader',
      },
      {
        test: /\.(ts|js)x?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
      {
        test: /\.(sass|scss|css)$/,
        use: [
          miniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'sass-loader',
            options: {
              implementation: require('sass'),
              sassOptions: {
                fiber: require('fibers'),
              },
            },
          },
        ],
      },
    ],
  },
  stats: {
    errorDetails: true,
  },
  optimization: {
    minimize: false,
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.vue', 'json'],
    alias: {
      vue: 'vue/dist/vue.esm-bundler.js',
    },
  },
  plugins: [
    new VueLoaderPlugin(),
    new HTMLWebpackPlugin({
      template: resolve('./index.html'),
      filename: 'index.html',
    }),
    new webpackBundleAnalyzer(),
    new miniCssExtractPlugin({
      filename: 'themes/[name].css',
    }),
  ],
  devServer: {
    port: 9999,
    open: true,
  },
}
module.exports = webpackConfig
